package com.edgclub.edg.activity;

import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentPagerAdapter;
import android.support.v4.view.ViewPager;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.edgclub.edg.R;
import com.edgclub.edg.fragment.ImageFragment;
import com.edgclub.edg.model.PostsAlbum;
import com.edgclub.edg.uc.ExtendedViewPager;

import java.util.ArrayList;
import java.util.List;

/**
 * 图集
 * 
 * @author eric
 * 
 */
public class ImageViewPagerActivity extends GzBaseActivity implements
		OnClickListener {

	private int preEnablePositon = 0; // 前一个被选中的点的索引位置 默认情况下为0

	private List<PostsAlbum> imageList=new ArrayList<>();
	private int position1;
	ExtendedViewPager mViewPager ;
	private LinearLayout viewGroup ;
	private TextView pass_tag;
	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_gz_llimage);
		imageList= (List<PostsAlbum>) getIntent().getSerializableExtra("urls");
		if(null==imageList)
			imageList=new ArrayList<>();
		position1=getIntent().getIntExtra("position",0);
		initView();
	}


	protected void initView() {
		mViewPager= (ExtendedViewPager) findViewById(R.id.view_pager);
		viewGroup = (LinearLayout)findViewById(R.id.viewGroup);
		pass_tag= (TextView) findViewById(R.id.pass_tag);
		mViewPager.setAdapter(new MyFragmentPagerAdapter(getSupportFragmentManager()));
		mViewPager.setCurrentItem(position1);
		mViewPager.setOnPageChangeListener(new MyOnPageChangeListener());
		pass_tag.setText(position1+1+"/"+imageList.size());
//		initDot();
	}

	/**
	 * 初始化小圆点
	 */
	private void initDot() {
		viewGroup.removeAllViews();
		preEnablePositon = 0;
		LinearLayout.LayoutParams localLayoutParams = new LinearLayout.LayoutParams(
				LinearLayout.LayoutParams.WRAP_CONTENT, LinearLayout.LayoutParams.WRAP_CONTENT);
		for (int i = 0; i < imageList.size(); i++) {
			ImageView dotImageView = new ImageView(this);
			dotImageView.setId(i);
			dotImageView.setScaleType(ImageView.ScaleType.FIT_CENTER);
			dotImageView.setLayoutParams(localLayoutParams);
			dotImageView.setPadding(5, 5, 5, 5);
			dotImageView.setImageResource(R.drawable.gallery_page_indicator_selector);

			if (i == 0) {
				dotImageView.setEnabled(true);//第一次选上第一个
			}else{
				dotImageView.setEnabled(false);
			}
			viewGroup.addView(dotImageView);
		}
	}


	@Override
	public void onClick(View view) {

	}

	class MyFragmentPagerAdapter extends FragmentPagerAdapter {

		public MyFragmentPagerAdapter(FragmentManager fm) {
			super(fm);
		}

		@Override
		public Fragment getItem(int position) {
			return ImageFragment.newInstance(imageList.get(position)) ;//  ;
		}

		@Override
		public int getCount() {
			return imageList.size();
		}
	}


	public class  MyOnPageChangeListener implements ViewPager.OnPageChangeListener {

		@Override
		public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {

		}

		@Override
		public void onPageSelected(int position) {
			// 取余后的索引
//			int newPositon = position ;

//			// 把上一个点设置为被选中
//			viewGroup.getChildAt(preEnablePositon).setEnabled(false);
//
//			// 根据索引设置那个点被选中
//			viewGroup.getChildAt(newPositon).setEnabled(true);
//			preEnablePositon = newPositon;
			pass_tag.setText(position+1+"/"+imageList.size());
		}

		@Override
		public void onPageScrollStateChanged(int state) {

		}
	}

}
